package com.proton.carepatchtemp.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.activity.HealthTipsActivity;
import com.proton.carepatchtemp.adapterholder.HealthTipsHolder;
import com.proton.carepatchtemp.constant.Extras;
import com.proton.carepatchtemp.database.table.HealthTip;
import com.proton.carepatchtemp.utils.FormatUtils;

import java.util.List;

/**
 * Created by MoonlightSW on 2017/2/22.
 */

public class HealthTipsAdapter extends RecyclerView.Adapter<HealthTipsHolder> {
    private Context mContext;
    private LayoutInflater mInflater;
    private List<HealthTip> mHealthTips;

    public HealthTipsAdapter(List datas, Context context) {
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
        this.mHealthTips = datas;
    }

    @Override
    public HealthTipsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        HealthTipsHolder holder = new HealthTipsHolder(mInflater.inflate(R.layout.item_health_tip, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(HealthTipsHolder holder, int position) {
        HealthTip temp = mHealthTips.get(position);
        if (null == temp) {
            return;
        }
        holder.itemView.setOnClickListener(v -> {
            Intent goHealthTips = new Intent(mContext, HealthTipsActivity.class);
            goHealthTips.putExtra(Extras.MSG_HEALTH_TIPS_URL, temp.getUrl());
            mContext.startActivity(goHealthTips);
        });
        holder.mTvMsgTitle.setText(temp.getTitle());
        holder.mTvMsgTime.setText(FormatUtils.transferLongToDate(mContext.getString(R.string.string_year_month_day_h_m_s), temp.getTime()));
        if (TextUtils.isEmpty(temp.getImage())) {
            holder.mSdvHtImg.setVisibility(View.GONE);
        } else {
            holder.mSdvHtImg.setVisibility(View.VISIBLE);
            holder.mSdvHtImg.setImageURI(temp.getImage());
        }
        holder.mTvHtContent.setText(temp.getSummary());
    }

    @Override
    public int getItemCount() {
        return mHealthTips == null ? 0 : mHealthTips.size();
    }
}
